<!doctype html>



  


<html class="theme-next muse use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.2" />






<meta name="description" content="CocoaPods是什么?CocoaPods是为我们的Xcode projects 管理库依赖的工具 。当你的project开发时需要引用第三方库的，你只需将project所依赖的库名添加到一个名为Podfile的纯文本文件中。CocoaPods将会获取project所依赖的库的源代码，并把这些库链接到同一个Xcode workspace来构建你的project。
安装CocoaPodsCocoa">
<meta property="og:type" content="article">
<meta property="og:title" content="CocoaPods(一)">
<meta property="og:url" content="http://lfcoder.github.io/2016/03/01/CocoaPods/index.html">
<meta property="og:site_name" content="只道是寻常">
<meta property="og:description" content="CocoaPods是什么?CocoaPods是为我们的Xcode projects 管理库依赖的工具 。当你的project开发时需要引用第三方库的，你只需将project所依赖的库名添加到一个名为Podfile的纯文本文件中。CocoaPods将会获取project所依赖的库的源代码，并把这些库链接到同一个Xcode workspace来构建你的project。
安装CocoaPodsCocoa">
<meta property="og:updated_time" content="2016-10-25T09:17:46.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="CocoaPods(一)">
<meta name="twitter:description" content="CocoaPods是什么?CocoaPods是为我们的Xcode projects 管理库依赖的工具 。当你的project开发时需要引用第三方库的，你只需将project所依赖的库名添加到一个名为Podfile的纯文本文件中。CocoaPods将会获取project所依赖的库的源代码，并把这些库链接到同一个Xcode workspace来构建你的project。
安装CocoaPodsCocoa">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="http://lfcoder.github.io/2016/03/01/CocoaPods/"/>


  <title> CocoaPods(一) | 只道是寻常 </title>
</head>

<body itemscope itemtype="//schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="//schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">只道是寻常</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle"></p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="//schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                CocoaPods(一)
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-03-01T10:04:24+08:00" content="2016-03-01">
              2016-03-01
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/category/技术/" itemprop="url" rel="index">
                    <span itemprop="name">技术</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="CocoaPods是什么"><a href="#CocoaPods是什么" class="headerlink" title="CocoaPods是什么?"></a>CocoaPods是什么?</h3><p>CocoaPods是为我们的Xcode projects 管理库依赖的工具 。当你的project开发时需要引用第三方库的，你只需将project所依赖的库名添加到一个名为<strong>Podfile</strong>的纯文本文件中。CocoaPods将会获取project所依赖的库的源代码，并把这些库链接到同一个Xcode workspace来构建你的project。</p>
<h3 id="安装CocoaPods"><a href="#安装CocoaPods" class="headerlink" title="安装CocoaPods"></a>安装CocoaPods</h3><p>CocoaPods是用脚本语言Ruby构建的。macOS默认是安装Ruby环境的，你可以使用系统自带的Ruby环境来安装CocoaPods。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ sudo gem install cocoapods</span><br></pre></td></tr></table></figure>
<p>注意在执行上面的命令之前,我们需要替换一下ruby的软件源。ruby官方的软件源是<code>https://rubygems.org/</code>,我们可以通过下面的命令查看当前ruby环境的软件源是什么</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ gem sources -l</span><br></pre></td></tr></table></figure>
<p>因为官方的软件源是被墙，所以我们要替换一下。操作如下：<br>首先删除默认的软件源</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ gem sources --remove https://rubygems.org/</span><br></pre></td></tr></table></figure>
<p>添加<a href="https://ruby-china.org/" target="_blank" rel="external">Ruby China</a>维护的软件源</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ gem sources -a https://gems.ruby-china.org/</span><br></pre></td></tr></table></figure>
<blockquote>
<p>注：之前淘宝也维护着RubyGems，但是现在不在维护，所以之前使用淘宝源的，应该替换成Ruby China的。</p>
</blockquote>
<p>安装了CocoaPods后，我们还需要执行下面的命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ pod setup</span><br></pre></td></tr></table></figure>
<p>当执行<code>pod setup</code>时，CocoaPods就创建~/.cocoapods/respos目录，然后从<a href="https://github.com/CocoaPods/Specs" target="_blank" rel="external">GitHub CocoaPods仓库</a>里把所有的Podspec克隆到该目录下（也就是把github上CocoaPods仓库的master分支拷贝下来）。由于拷贝的文件较大（一百多兆），需要稍等会！</p>
<blockquote>
<p>what is PodSpec？后面我们后讲到</p>
</blockquote>
<h3 id="更新CocoaPods"><a href="#更新CocoaPods" class="headerlink" title="更新CocoaPods"></a>更新CocoaPods</h3><p>如果你要更新CocoaPods的版本,同安装命令一样</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ sudo gem install cocoapods</span><br></pre></td></tr></table></figure>
<h3 id="在项目中使用CocoaPods"><a href="#在项目中使用CocoaPods" class="headerlink" title="在项目中使用CocoaPods"></a>在项目中使用CocoaPods</h3><p>在macOS上已经安装了CocoaPods。接下来我们来学习怎么样在Xcode中使用CocoaPods。对于在项目中使用CocoaPods可以按照下面的步骤：</p>
<ul>
<li>创建你的Xcode project</li>
<li>打开终端，使用<code>$ cd</code>命令进入你创建的project根目录</li>
<li>创建一个名为<em>Podfile</em>的文件，对于创建该文件可以使用命令<code>$ pod init</code></li>
<li><p>打开Podfile文件，第一行指定平台和支持的版本</p>
<pre><code>platform : ios, &apos;9.0&apos;
</code></pre></li>
<li><p>为了使用CocoaPods, 你应该声明Xcode project中的哪个Xcode target 和CocoaPods链接。</p>
<pre><code>target &apos;$TATGET_NAME&apos; do

end
</code></pre></li>
<li><p>在Podfile文件中单独一行通过<code>pod &#39;$PODNAME&#39;</code>来添加一个pod</p>
<pre><code>   target &apos;MyApp&apos; do
   pod &apos;AFNetworking&apos;
end
</code></pre></li>
<li><p>编辑完了<em>Podfile</em>文件，进行保存。</p>
</li>
<li>运行 <code>$ pod install</code></li>
<li>这时候你就会发现你的project目录下面多了一个<strong>MyApp.xcworkspace</strong>文件。然后打开这个文件就可以码你的project了。</li>
</ul>
<h3 id="使用pod-install-vs-pod-update"><a href="#使用pod-install-vs-pod-update" class="headerlink" title="使用pod install vs pod update"></a>使用<code>pod install</code> vs <code>pod update</code></h3><p>许多人对什么时候<code>pod install</code> 和 <code>pod update</code>是迷惑的。他们经常在该使用<code>pod install</code>的时候使用了<code>pod update</code>。<br>这张就来讨论一下两者的区别：</p>
<ul>
<li>使用<code>pod install</code>是用来安装新的pod到你的project中的。即使你之前已经有<em>Podfile</em>文件并且运行了<code>pod install</code>。因此在已经使用了CocoaPods的项目中，<code>pod install</code>只是向你的project中添加或者删除pod。</li>
<li>使用<code>pod update [POD_NAME]</code>只是在你需要pods更新到一个新版本的时候</li>
</ul>
<blockquote>
<p>注意：<code>pod install</code>和<code>pod update</code>都不是作用于CocoaPods这个工具软件的安装和更新。而是作用于<em>Podfile</em>文件中描述的pod（就是project所依赖的第三方库）的安装和更新。</p>
</blockquote>
<h4 id="pod-install"><a href="#pod-install" class="headerlink" title="pod install"></a>pod install</h4><p>这个命令在两个时候用：</p>
<ol>
<li><p>第一次为你的project安装pod的时候</p>
</li>
<li><p>当你再次编辑<em>Podfile</em>，在文件中添加，删除，更新pods的时候</p>
</li>
</ol>
<p> 每次运行<code>pod install</code>的时候，CocoaPods就会为project去下载并安装pods。安装完毕后，CocoaPods会将每个pod的版本信息写入一个名为<em>Podfile.lock</em>的文件中。这个文件记录这每个pod的版本并锁着这些版本pod。</p>
<p>当运行<code>pod install</code>时，CocoaPods只解决在<em>Podfile.lock</em>中没有记录的pods的依赖问题。</p>
<ul>
<li>如果pod在<em>Podfile.lock</em>中记录了，那么CococaPods就会下载Podfile.lock记录的某个版本的pod,不会试图去检查pod是否有可有的新版本。</li>
<li>如果pod没有在<em>Podfile.lock</em>中记录，CocoaPods将会根据<em>Podfile</em>描述的版本检索相应的pod进行安装</li>
</ul>
<h4 id="pod-outdated"><a href="#pod-outdated" class="headerlink" title="pod outdated"></a>pod outdated</h4><p>如果你运行<code>pod outdated</code>,CocoaPods就会罗列出<em>Podfile.lock</em>中所记录pod（也就是当前project已经安装的pods）的新版本。这样你就知道了哪些pods有了新版本，以便于运行<code>pod update $PODNAME</code>来更新项目中的pods。</p>
<blockquote>
<p>注：运行<code>pod update $PODNAME</code>来更新pod时，新版pod的版本号必须满足<em>Podfile</em>对pod的设置，例如<code>pod &#39;MyPod&#39;, &#39;~&gt;x.y&#39;</code>。</p>
</blockquote>
<h4 id="pod-update"><a href="#pod-update" class="headerlink" title="pod update"></a>pod update</h4><p>当你运行<code>pod update $PODNAME</code>的时候，CocoaPods试图找到名字为PODNAME的pod的新版本进行安装，不会考虑<em>Podfile.lock</em>中锁定的pod的。</p>
<p>如果运行<code>pod update</code>，那么CocoaPods将会试图更新<em>Podfile</em>中列举所有的pod。</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/04/04/学习使用Hexo写博客/" rel="prev" title="学习使用Hexo写博客">
                学习使用Hexo写博客 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="//schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/uploads/avatar.jpg"
               alt="Wdluya" />
          <p class="site-author-name" itemprop="name">Wdluya</p>
          <p class="site-description motion-element" itemprop="description">人之所以痛苦,在于追求错误的东西</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">3</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">1</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#CocoaPods是什么"><span class="nav-number">1.</span> <span class="nav-text">CocoaPods是什么?</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#安装CocoaPods"><span class="nav-number">2.</span> <span class="nav-text">安装CocoaPods</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#更新CocoaPods"><span class="nav-number">3.</span> <span class="nav-text">更新CocoaPods</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#在项目中使用CocoaPods"><span class="nav-number">4.</span> <span class="nav-text">在项目中使用CocoaPods</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用pod-install-vs-pod-update"><span class="nav-number">5.</span> <span class="nav-text">使用pod install vs pod update</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#pod-install"><span class="nav-number">5.1.</span> <span class="nav-text">pod install</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#pod-outdated"><span class="nav-number">5.2.</span> <span class="nav-text">pod outdated</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#pod-update"><span class="nav-number">5.3.</span> <span class="nav-text">pod update</span></a></li></ol></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Wdluya</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.2"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.2"></script>



  



  




  
  

  

  

  

</body>
</html>
